package com.caine.androidtask.ls20;
import java.io.File;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.caine.androidtask.R;
import com.caine.androidtask.launcher.Utils;


public class FileBrowser extends Activity implements OnItemClickListener{

	private List<Beans> list = null;
	private GridView gv;

	public String location = "/";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.caine_ls20);
		gv = (GridView) findViewById(R.id.gv_main);
		filesBrowser("/");
		gv.setOnItemClickListener(this);
		
	}

	public class ImageAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = null;
			LayoutInflater li = getLayoutInflater();
			view = li.inflate(R.layout.caine_ls20_01, null);
			ImageView iv = (ImageView) view.findViewById(R.id.file_icon);
			TextView tv = (TextView) view.findViewById(R.id.file_name);
			iv.setImageResource(list.get(position).getIcon());
			tv.setText(list.get(position).getName());
			tv.setLines(2);
			
			return view;
		}

	}

	public void filesBrowser(String path) {
		List<Beans> listIsNull = FileHelper.getFilesList(path);
		File f = new File(path);
		 if(f.isDirectory()){
		if (listIsNull == null) {
			Toast.makeText(this, "Cann't open this file!", Toast.LENGTH_SHORT)
					.show();
			
			//list = null;
		} else {
			// Log.e(Utils.TAG, "list size : " + list.size());
			
			
			setTitle(path.equals("/")?"/":location);
			list = listIsNull;
			gv.setAdapter(new ImageAdapter());
		}
		}else{
			startActivity(FileHelper.openFile(path));
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		 if(KeyEvent.KEYCODE_BACK == keyCode){
			if(location.equals("/")){
				exit();
				return true;
			}	
			File file = new File(location);
			location = file.getParent();
			filesBrowser(location);
			 return true;
		 }
		return super.onKeyDown(keyCode, event);
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		location = list.get(arg2).getPath();
		if(location!=null)
		filesBrowser(location);
	}
	
	private void exit(){
		 new AlertDialog.Builder(this)
		 .setMessage("Are you sure exit?")
		 .setCancelable(false)
		 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				FileBrowser.this.finish();
				android.os.Process.killProcess(android.os.Process.myPid());
				System.exit(0);
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		}).create().show();
	}
	
	

}
